package ase.assignment.sts.db.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ase.assignment.sts.api.CommonHandler;
import ase.assignment.sts.api.StockHandler;
import ase.assignment.sts.beans.StockAssertEntity;
import ase.assignment.sts.beans.StockQuoteEntity;
import ase.assignment.sts.db.DBHandler;

public class StockHandlerImpl implements StockHandler {

	StockHandlerImpl() {

	}

	private void checkStock(StockAssertEntity stock) {
		if (stock.getName() == null) {
			stock.setName("NO_NAME");
		}
		if (stock.getAmount() < 0) {
			stock.setAmount(0);
		}
		if (stock.getInitPrice() < 0) {
			stock.setInitPrice(0);
		}
	}

	public void create(StockAssertEntity stock) {
		checkStock(stock);
		String sql = "insert into Asserts ( id, name, portfolio, amount, init_price ) values("
				+ stock.getId()
				+ ", '"
				+ stock.getName()
				+ "', "
				+ stock.getPortfolio()
				+ ", "
				+ stock.getAmount()
				+ ", "
				+ stock.getInitPrice() + ")";
		try {
			System.out.println(sql);
			DBHandler.getInstance().createStatement().executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHandler.getInstance().dispose();
		}
	}

	public void delete(int stockId) {
		String sql = "delete from Asserts where id = " + stockId;
		try {
			System.out.println(sql);
			DBHandler.getInstance().createStatement().executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHandler.getInstance().dispose();
		}
	}

	public void update(StockAssertEntity stock) {
		checkStock(stock);
		String sql = "update Asserts set name='" + stock.getName()
				+ "', portfolio=" + stock.getPortfolio() + ", amount="
				+ stock.getAmount() + ", init_price=" + stock.getInitPrice()
				+ " where id = " + stock.getId() + "";
		try {
			System.out.println(sql);
			DBHandler.getInstance().createStatement().executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHandler.getInstance().dispose();
		}
	}

	public StockAssertEntity findById(int id) {
		String sql = "select name, portfolio, amount, init_price from Asserts where id="
				+ id;
		try {
			System.out.println(sql);
			ResultSet rs = DBHandler.getInstance().createStatement()
					.executeQuery(sql);
			if (rs != null && rs.next()) {
				StockAssertEntity entity = new StockAssertEntity(id);
				entity.setName(rs.getString(1).trim());
				entity.setPortfolio(rs.getInt(2));
				entity.setAmount(rs.getInt(3));
				entity.setInitPrice(rs.getDouble(4));
				return entity;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHandler.getInstance().dispose();
		}
		return null;
	}

	public StockAssertEntity[] getAllStocks(int porId) {
		String sql = "select id, name, amount, init_price from Asserts where portfolio="
				+ porId;
		try {
			System.out.println(sql);
			ResultSet rs = DBHandler.getInstance().createStatement()
					.executeQuery(sql);
			if (rs != null) {
				List<StockAssertEntity> list = new ArrayList<StockAssertEntity>();
				while (rs.next()) {
					StockAssertEntity entity = new StockAssertEntity(rs
							.getInt(1));
					entity.setName(rs.getString(2).trim());
					entity.setAmount(rs.getInt(3));
					entity.setInitPrice(rs.getDouble(4));
					entity.setPortfolio(porId);
					list.add(entity);
				}
				return list.toArray(new StockAssertEntity[list.size()]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHandler.getInstance().dispose();
		}
		return null;
	}

	public StockQuoteEntity getCurrentPrice(int stockId) {
		StockQuoteEntity quote = new StockQuoteEntity(stockId);
		StockAssertEntity asserts = findById(stockId);
		quote.setTime(new Date());
		if (asserts != null) {
			double price = CommonHandler.calculateQuote(asserts.getInitPrice());
			quote.setPrice(price);
		}
		return quote;
	}
}
